package cn.px.monthly.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.px.common.core.utils.StringUtils;
import cn.px.monthly.domain.CalculatePrice;
import cn.px.monthly.domain.ParkingInfo;
import cn.px.monthly.domain.Rule;
import cn.px.monthly.domain.vo.MaskDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.px.common.log.annotation.Log;
import cn.px.common.log.enums.BusinessType;
import cn.px.common.security.annotation.RequiresPermissions;
import cn.px.monthly.domain.RuleParking;
import cn.px.monthly.service.IRuleParkingService;
import cn.px.common.core.web.controller.BaseController;
import cn.px.common.core.web.domain.AjaxResult;
import cn.px.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.px.common.core.web.page.TableDataInfo;

/**
 * 包月套餐与车场关联Controller
 *
 * @author 品讯科技
 * @date 2024-08
 */
@Api(tags = "包月套餐与车场关联")
@RestController
@RequestMapping("/rule-parking")
public class RuleParkingController extends BaseController {
    @Autowired
    private IRuleParkingService ruleParkingService;

    /**
     * 查询包月套餐与车场关联列表
     */
    @ApiOperation(value = "查询包月套餐与车场关联列表", response = RuleParking.class)
    @RequiresPermissions("monthly:parking:list")
    @GetMapping("/list")
    public TableDataInfo list(RuleParking ruleParking) {
        startPage();
        List<RuleParking> list = ruleParkingService.selectRuleParkingList(ruleParking);
        return getDataTable(list);
    }

    /**
     * 导出包月套餐与车场关联列表
     */
    @ApiOperation(value = "导出包月套餐与车场关联列表")
    @RequiresPermissions("monthly:parking:export")
    @Log(title = "包月套餐与车场关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RuleParking ruleParking) {
        List<RuleParking> list = ruleParkingService.selectRuleParkingList(ruleParking);
        ExcelUtil<RuleParking> util = new ExcelUtil<RuleParking>(RuleParking.class);
        util.exportExcel(response, list, "包月套餐与车场关联数据");
    }

    /**
     * 获取包月套餐与车场关联详细信息
     */
    @ApiOperation(value = "获取包月套餐与车场关联详细信息", response = RuleParking.class)
    @RequiresPermissions("monthly:parking:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ruleParkingService.selectRuleParkingById(id));
    }

    /**
     * 新增包月套餐与车场关联
     */
    @ApiOperation(value = "新增包月套餐与车场关联")
    @RequiresPermissions("monthly:parking:add")
    @Log(title = "包月套餐与车场关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RuleParking ruleParking) {
        return toAjax(ruleParkingService.insertRuleParking(ruleParking));
    }

    /**
     * 修改包月套餐与车场关联
     */
    @ApiOperation(value = "修改包月套餐与车场关联")
    @RequiresPermissions("monthly:parking:edit")
    @Log(title = "包月套餐与车场关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RuleParking ruleParking) {
        return toAjax(ruleParkingService.updateRuleParking(ruleParking));
    }

    /**
     * 删除包月套餐与车场关联
     */
    @ApiOperation(value = "删除包月套餐与车场关联")
    @RequiresPermissions("monthly:parking:remove")
    @Log(title = "包月套餐与车场关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ruleParkingService.deleteRuleParkingByIds(ids));
    }


    /**
     * 获取参加包月活动的车场信息
     */
    @ApiOperation(value = "获取参加包月活动的车场信息", response = ParkingInfo.class)
    @GetMapping("/getRuleParking")
    public AjaxResult getRuleParking(String parkName, String area) {
        List<ParkingInfo> list = ruleParkingService.getRuleParkingList(parkName, area);
        return AjaxResult.success(list);
    }


    /**
     * 通过车场Id获取包月套餐
     */
    @ApiOperation(value = "通过车场Id获取包月套餐", response = Rule.class)
    @GetMapping("/getRuleByParkingId")
    public AjaxResult getRuleByParkingId(String parkIds) {
        if (StringUtils.isBlank(parkIds)) {
            return AjaxResult.success(null);
        }
        List<Rule> list = ruleParkingService.getRuleByParkingId(parkIds);
        return AjaxResult.success((null != list && list.size() == 1) ? list.get(0) : null);
    }

    /**
     * 计算优惠金额
     */
    @ApiOperation(value = "计算优惠金额", response = Rule.class)
    @PostMapping("/calculatePrice")
    public AjaxResult calculatePrice(@RequestBody CalculatePrice calculatePrice) {
        BigDecimal bigDecimal = ruleParkingService.calculatePrice(calculatePrice);

        return AjaxResult.success(bigDecimal);
    }

    /**
     * 模拟器
     */
    @ApiOperation(value = "模拟器")
    @PostMapping("/simulator")
    public AjaxResult simulator(@RequestBody MaskDataVO maskDataVO) {
        Map<String, BigDecimal> map = new HashMap<>();
        if (null == maskDataVO || StringUtils.isBlank(maskDataVO.getParkingIds())
                || null == maskDataVO.getRuleId() || null == maskDataVO.getNum()) {
            return AjaxResult.error("参数不能为空");
        }
        List<ParkingInfo> list = ruleParkingService.getRuleParkingByParkingIds(maskDataVO.getParkingIds());
        BigDecimal totalPrice = list.stream().map(p -> p.getPrice().multiply(BigDecimal.valueOf(maskDataVO.getNum()))).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
        CalculatePrice calculatePrice = new CalculatePrice();
        calculatePrice.setRuleId(maskDataVO.getRuleId());
        calculatePrice.setTotalPrice(totalPrice);
        calculatePrice.setCount(list.size());
        BigDecimal preferentialPrice = ruleParkingService.calculatePrice(calculatePrice);
        map.put("totalPrice", totalPrice.setScale(2, RoundingMode.HALF_UP));
        map.put("preferentialPrice", preferentialPrice.setScale(2, RoundingMode.HALF_UP));
        map.put("realPrice", totalPrice.subtract(preferentialPrice).setScale(2, RoundingMode.HALF_UP));
        return AjaxResult.success(map);
    }


}
